$(function() {
    var tit_user = getCookie("user");
    if (tit_user) {
        $(".adad").hide();
        $(".hy").show().text(`${tit_user},欢迎你`);
        $(".hy").click(function() {
            if (confirm("确定退出登录吗?")) {
                setCookie("user", `${tit_user}`, day = -1, path = "/");
                $(".adad").show();
                $(".hy").hide();
            }
        })
    }

    //判断是否有cookie 并跳我的收藏页面
    $(".nav_list li").eq(3).click(function() {
        if (document.cookie) {
            location.href = "../html/myLikeGoods.html";
        } else {
            alert("请先登录");
        }
    })
    var user = document.cookie.split("=")[1];
    $.ajax({
        type: "get",
        url: "../php/shoppingCart_2.php",
        data: {
            user
        },
        dataType: "json",
        success: function(result) {
            var { status, msg, list } = result;
            var html = "";
            console.log(list);
            list.forEach(({ id, goodsImg, goodsName, goodsPrice, num, userName, goodsId }) => {
                var addPrice = num * parseFloat(goodsPrice).toFixed(2);
                html += `<div class="list-item" data_id="${id}">
                <div class="col col-check">
                    <label>
                        <input type="checkbox" class="check-one">
                    </label>
                </div>
                <div class="col col-img">
                    <img src="${goodsImg}" alt="">
                </div>
                <div class="col col-detail">${goodsName}</div>
                <div class="col col-price">${goodsPrice}</div>
                <div class="col col-count">
                    <div class="countBox">
                        <span class="reduce">-</span>
                        <span class="countSpan">${num}</span>
                        <span class="add">+</span>
                    </div>
                </div>
                <div class="col col-subtotal">${addPrice}元</div>
                <div class="col col-operate"><i class="iconfont icon-close delete"></i></div>
            </div>`

            });
            $(".list-body").html(html);





            // ---------------------------------------------
            // 全选
            $(".check-all").click(function() {
                var status = $(this).prop("checked");
                $(".check-one").prop("checked", status);
                getTotal()
            })

            //单选
            $(".check-one").click(function() {
                isAllChecked()
                getTotal()
            })

            //商品数量加
            $(".add").click(async function() {
                var num = ($(this).prev().html() * 1);
                num++
                $(this).prev().html(num);
                var price = $(this).parent().parent().prev().text();
                var subtotal = (price * num).toFixed(1);
                $(this).parent().parent().next().html(subtotal + " 元");

                var nums = $(this).prev().html();
                var id = $(this).parents(".list-item").attr("data_id");
                getTotal()
                $.ajax({
                    type: "get",
                    url: "../php/shoppingCart_jiesuan.php",
                    data: {
                        nums,
                        id
                    },
                    dataType: "json",
                    success: function(result) {
                        var { msg, status } = result;

                    }
                })

            })

            //商品数量减
            $(".reduce").click(function() {
                var num = $(this).next().html() * 1;
                if (num <= 1) return false;
                num--;
                if (num == 1) $(this).text("-");
                $(this).next().html(num);
                var price = $(this).parent().parent().prev().text();
                var subtotal = (price * num).toFixed(1);
                $(this).parent().next().text(subtotal);
                $(this).parent().parent().next().html(subtotal + "元");
                getTotal()

                var nums = $(this).next().html();
                var id = $(this).parents(".list-item").attr("data_id");
                $.ajax({
                    type: "get",
                    url: "../php/shoppingCart_jiesuan.php",
                    data: {
                        nums,
                        id
                    },
                    dataType: "json",
                    success: function(result) {
                        var { msg, status } = result;

                    }
                })
            })

            //删除商品
            $(".delete").click(function() {
                if (confirm("确定要删除吗")) {
                    $(this).parents(".list-item").remove();
                    isAllChecked()
                    getTotal()
                    var id = $(this).parents(".list-item").attr("data_id");
                    $.ajax({
                        type: "get",
                        url: "../php/shoppingCart_del.php",
                        data: {
                            id
                        },
                        dataType: "json",
                        success: function(result) {
                            var { status, msg } = result;
                            if (status) {

                                //删除商品
                                $(".delete").click(function() {
                                    $(this).parents(".list-item").remove();
                                    isAllChecked()
                                    getTotal()
                                })

                            }
                        }
                    })
                }
            })




            function isAllChecked() {
                var flag = $(".check-one").is(":not(:checked)");
                // $(".check-all").prop("checked", !flag);
                $(".check-all").prop("checked", $(".check-one").length == 0 ? false : !flag);
            }


            function getTotal() {
                var sum = 0;
                var allPrice = 0;
                $(".check-one:checked").parents(".list-item").each(function() {
                    var num = $(this).find(".countSpan").html() * 1;
                    var subtotal = parseInt($(this).find(".col-subtotal").html());
                    sum += num;
                    allPrice += subtotal;
                })
                $(".selectedNum").html(sum);
                $(".selectedTotal").html(allPrice);
                $(".totalNum").html(sum);



                var price_add = 0;
                console.log($(".countSpan"));
                $(".countSpan").each(function(index) {
                    price_add += $(".countSpan").eq(index).html() * 1;
                })
                $(".totalNum").html(price_add)
            }

        }
    })



})